package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private  LeaseTermMapper leaseTermMapper;
   @Autowired
   private ApartmentInfoMapper apartmentInfoMapper;
   @Autowired
   private GraphInfoMapper graphInfoMapper;
   @Autowired
   private AttrValueMapper attrValueMapper;
   @Autowired
   private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private RoomLabelService roomLabelService;
    @Autowired
    private RoomFacilityService roomFacilityService;
    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    private RoomAttrValueService roomAttrValueService;
    @Autowired
    private RoomLeaseTermService roomLeaseTermService;

    @Override
    public void saveOrUpdateroom(RoomSubmitVo roomSubmitVo) {
        boolean isUpdate = roomSubmitVo.getId() != null;
        super.saveOrUpdate(roomSubmitVo);

        //若为更新操作,则先删除与Room相关的各项信息列表
        if (isUpdate) {
            //1.删除原有的graphInfoList "图片列表"
            LambdaQueryWrapper<GraphInfo> graphQueryWrapper = new LambdaQueryWrapper<>();
            graphQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
            graphQueryWrapper.eq(GraphInfo::getItemId, roomSubmitVo.getId());
            graphInfoService.remove(graphQueryWrapper);

            //2.删除原有的roomAttrValueList "属性信息列表"
            LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId, roomSubmitVo.getId());
            roomAttrValueService.remove(roomAttrValueLambdaQueryWrapper);

            //3.删除原有roomLabelList "标签信息列表"
            LambdaQueryWrapper<RoomLabel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(RoomLabel::getRoomId, roomSubmitVo.getId());
            roomLabelService.remove(lambdaQueryWrapper);

            //4.删除原有roomFacilityLIst "配套信息列表"
            LambdaQueryWrapper<RoomFacility> facilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
            facilityLambdaQueryWrapper.eq(RoomFacility::getRoomId, roomSubmitVo.getId());
            roomFacilityService.remove(facilityLambdaQueryWrapper);

            //5.删除出原有paymentTypeList  "支付方式列表"
            LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getPaymentTypeId, roomSubmitVo.getId());
            roomPaymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);

            //6.删除原有leaseTermList  "可选租期列表"
            LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId, roomSubmitVo.getId());
            roomLeaseTermService.remove(roomLeaseTermLambdaQueryWrapper);

            //保存更新graphInfoList
            List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
            if (!CollectionUtils.isEmpty(graphVoList)) {
                ArrayList<GraphInfo> graphInfoList = new ArrayList<>();
                for (GraphVo graphVo : graphVoList) {
                    GraphInfo graphInfo = new GraphInfo();
                    graphInfo.setItemType(ItemType.ROOM);
                    graphInfo.setItemId(roomSubmitVo.getId());
                    graphInfo.setName(graphVo.getName());
                    graphInfo.setUrl(graphInfo.getUrl());
                    graphInfoList.add(graphInfo);
                }
                graphInfoService.saveBatch(graphInfoList);
            }
            //2.保存新的roomAttrValueList
            List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
            if (!CollectionUtils.isEmpty(attrValueIds)) {
                List<RoomAttrValue> roomAttrValueList = new ArrayList<>();
                for (Long attrValueId : attrValueIds) {
                    RoomAttrValue roomAttrValue = RoomAttrValue.builder().roomId(roomSubmitVo.getId()).attrValueId(attrValueId).build();
                    roomAttrValueList.add(roomAttrValue);
                }
                roomAttrValueService.saveBatch(roomAttrValueList);
            }
            //3.保存新的facilityInfoList
            List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
            if (!CollectionUtils.isEmpty(facilityInfoIds)) {
                ArrayList<RoomFacility> roomFacilityList = new ArrayList<>();
                for (Long facilityInfoId : facilityInfoIds) {
                    RoomFacility roomFacility = RoomFacility.builder().roomId(roomSubmitVo.getId()).facilityId(facilityInfoId).build();
                    roomFacilityList.add(roomFacility);
                }
                roomFacilityService.saveBatch(roomFacilityList);
            }
            //4. 保存新的labelInfoList
            List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
            if (!CollectionUtils.isEmpty(labelInfoIds)) {
                ArrayList<RoomLabel> roomLabelArrayList = new ArrayList<>();
                for (Long labelInfoId : labelInfoIds) {
                    RoomLabel roomLabel = RoomLabel.builder().labelId(labelInfoId).roomId(roomSubmitVo.getId()).build();
                    roomLabelArrayList.add(roomLabel);

                }
                roomLabelService.saveBatch(roomLabelArrayList);
            }
            //5.保存新的paymentTypeList
            List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
            if (!CollectionUtils.isEmpty(paymentTypeIds)) {
                ArrayList<RoomPaymentType> roomPaymentTypeList = new ArrayList<>();
                for (Long paymentTypeId : paymentTypeIds) {
                    RoomPaymentType roomPaymentType = RoomPaymentType.builder().roomId(roomSubmitVo.getId()).paymentTypeId(paymentTypeId).build();
                    roomPaymentTypeList.add(roomPaymentType);
                }
                roomPaymentTypeService.saveBatch(roomPaymentTypeList);
            }
            //6.保存新的leaseTermList
            List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
            if (!CollectionUtils.isEmpty(leaseTermIds)) {
                ArrayList<RoomLeaseTerm> roomLeaseTerms = new ArrayList<>();
                for (Long leaseTermId : leaseTermIds) {
                    RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder().leaseTermId(leaseTermId).roomId(roomSubmitVo.getId()).build();
                    roomLeaseTerms.add(roomLeaseTerm);
                }
                roomLeaseTermService.saveBatch(roomLeaseTerms);
            }

        }
    }

    @Override
    public IPage<RoomItemVo> pageRoomItemByQuery(Page<RoomItemVo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.pageRoomItemByQuery(page, queryVo);
    }

    @Override
    public RoomDetailVo getRoomDetaiById(Long id) {
        //1.查询RoomInfo
        RoomInfo roomInfo = roomInfoMapper.selectById(id);
        //2.查询所属公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(id);
        //3.查询graphInfoMapper
        List<GraphVo> graphVoList = graphInfoMapper.selectListByTtemTypeAndId(ItemType.ROOM, id);
        //4.查询attrValueList
        List<AttrValueVo> attrValueVoList = attrValueMapper.selectListByRoomId(id);
        //5.查询facilityInfoList
        List<FacilityInfo>facilityInfoList= facilityInfoMapper.selectListByRoomId(id);
        //6.查询labelInfoList
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListByApartmentId(id);
        //7.查询paymentTypeList
        List<PaymentType> paymentTypes = paymentTypeMapper.selectListByRoomId(id);
        //8.查询leaseTermList
        List<LeaseTerm>leaseTerms = leaseTermMapper.selectListByRoomId(id);


        RoomDetailVo adminRoomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo,adminRoomDetailVo);

        adminRoomDetailVo.setApartmentInfo(apartmentInfo);
        adminRoomDetailVo.setGraphVoList(graphVoList);
        adminRoomDetailVo.setApartmentInfo(apartmentInfo);
        adminRoomDetailVo.setAttrValueVoList(attrValueVoList);
        adminRoomDetailVo.setFacilityInfoList(facilityInfoList);
        adminRoomDetailVo.setPaymentTypeList(paymentTypes);
        adminRoomDetailVo.setLeaseTermList(leaseTerms);
        return adminRoomDetailVo;
    }

    @Override
    public void removeRoomById(Long id) {
        //删除roomInfo
        removeById(id);
        //删除grahpInfoList
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
       graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType,ItemType.ROOM);
       graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,id);
       graphInfoService.remove(graphInfoLambdaQueryWrapper);

       //删除attrValueList
        LambdaQueryWrapper<RoomAttrValue> attrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
       attrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId,id);
       roomAttrValueService.remove(attrValueLambdaQueryWrapper);

       //删除facilityInfoList
        LambdaQueryWrapper<RoomFacility> facilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        facilityLambdaQueryWrapper.eq(RoomFacility::getRoomId,id);
        roomFacilityService.remove(facilityLambdaQueryWrapper);

        //删除labelInfoList
        LambdaQueryWrapper<RoomLabel> labelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        labelLambdaQueryWrapper.eq(RoomLabel::getRoomId,id);
        roomLabelService.remove(labelLambdaQueryWrapper);

        //删除paymentTypeList
        LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId,id);
        roomPaymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);

        //删除leaseTermList
        LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId,id);
        roomLeaseTermService.remove(roomLeaseTermLambdaQueryWrapper);
    }
}




